// pages/scanimg/result.js
let request = require('../../wechat-lib/request')
let login = require('../../wechat-lib/login')
let common = require('../../wechat-lib/common')
const config = require('../../wechat-lib/config')
Page({
  mixins: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      url:decodeURIComponent(options.url),
      id:decodeURIComponent(options.id),
      appId:config.appId
   })
  },
  onShow:function(){
    if(!this.data.resultText){
      login.getOpenId().then(
        success=>{
          console.log("openId",success)
          const {openId}=success;
          request.yunrequest("wxv2","getPictureText",{picture:this.data.url,openId:openId,id:this.data.id,appId:config.appId}).then(res=>{
              if(res.risky){
                wx.hideShareMenu({
                  menus: ['shareAppMessage', 'shareTimeline']
                })
                wx.showToast({
                  title: '内容中含敏感内容，请修改后提交！',
                  icon: "none"
                })
              }else{
                wx.showShareMenu({
                  menus: ['shareAppMessage', 'shareTimeline']
                })
                let resultText="";
                let resultHtml="";
                for( let x in res.paragraphs_result){
                  console.log(res.paragraphs_result[x].words_result_idx);
                  resultHtml += "<p>";
                  for( let y in res.paragraphs_result[x].words_result_idx){
                    let index=res.paragraphs_result[x].words_result_idx[y]
                    resultText += res.words_result[index].words
                    resultHtml += res.words_result[index].words
                  }
                  resultText += "\r\n"
                  resultHtml +="</p>"
                }
                
                console.log(resultText);
                this.setData({ resultText, resultHtml});
              }
              
            }
          ,res=>{
            wx.showToast({
              title: '识别出错',
              content:'原因'+res
            })
          })
        },
        fail=>{
          fail && common.showErrorMsg(fail)
        }
      )
    }
  },
  copy:function (e){
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.content,
      success:function (res){
        wx.showToast({
          title: '复制成功',
        });
        setTimeout(function (){
          wx.hideToast()
        },2000)
      },
      fail:(res)=>{
        wx.showToast({
          title: '复制失败',
          content:res
        })
      }
    })
  },
  cleanSelect:function(){
    wx.navigateBack({
      delta: 0,
    })
    wx.showToast({
      "title": '请上传新图片,进行识别',
      "duration": 1000
    })
  },
  onShareAppMessage:()=>{
    return {
      title:"见图识文",
      desc:"识别图片上文字真准确，还能复制转发。"
    }
  },
  onShareTimeline:function(){
    return {
      title:"见图识文 识别图片上文字真准确，还能复制转发。",
      query:"url="+encodeURIComponent(this.data.url),
    }
  }
})